package com.hiwan.system.web.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.framework.dao.plugin.DataTablesUtil;
import org.framework.session.SessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.util.DateUtil;
import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.cache.OrgCacheService;
import com.hiwan.system.cache.SysCacheService;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.enumeration.ReturnCodeEnum;
import com.hiwan.system.pojo.DeptInfo;
import com.hiwan.system.pojo.OrgCacheInfo;
import com.hiwan.system.pojo.OrgInfo;
import com.hiwan.system.pojo.RoleInfo;
import com.hiwan.system.pojo.ParameterInfo;
import com.hiwan.system.pojo.ReturnInfo;
import com.hiwan.system.pojo.UserCacheInfo;
import com.hiwan.system.pojo.UserInfo;
import com.hiwan.system.pojo.UserRelation;
import com.hiwan.system.pojo.tree.TreeNode;
import com.hiwan.system.service.dept.DeptService;
import com.hiwan.system.service.org.OrgService;
import com.hiwan.system.service.user.UserService;
import com.hiwan.system.util.ConfigUtil;
import com.hiwan.system.util.DictCacheUtils;
import com.hiwan.system.util.UserCacheUtils;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	
	
	@Autowired
	private OrgService orgService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private SysCacheService sysCacheService;
	@Autowired
	private UserService userService;
	/*@Autowired
	private RoleService roleService;*/
	@Autowired
	private OrgCacheService orgCacheService;
	
//	@Autowired
//	private MenuService menuService;
	
	

	@RequestMapping("/frame.do")
	public String frame(ModelMap model) {
		return "user/frame";
	}

	@RequestMapping("/toQuery.do")
	public String toQuery(ModelMap model,HttpSession session) {
		return "user/query";
	}
	
	@RequestMapping("/privateFrame.do")
	public String toPrivateFrame(ModelMap model,HttpSession session){
	   return "user/privateFrame";
	}
	
	@RequestMapping("/getUserTree.do")
	public String getOrgTree(String rootOrgId, @RequestParam("checkType") String checkType, ModelMap model,
			HttpSession session) {
		
		if (StringUtils.isEmpty(rootOrgId)) {
			rootOrgId = String.valueOf(UserCacheUtils.getOrgId());
		}

		Map<String, TreeNode> childrenMap = new HashMap<String, TreeNode>();
		childrenMap.put(rootOrgId, buildTreeNode(rootOrgId, "org"));
		//childrenMap.putAll(getOrgChildren(rootOrgId));

		model.put("childrenMap", childrenMap);
		model.put("rootOrgId", rootOrgId);
		model.put("checkType", checkType);
		
		return "user/userTree";
	}
	@RequestMapping("/list")
	public String list( UserInfo userInfo, ModelMap model) {
		ParameterInfo param = sysCacheService.getSysParameter("dept_yn","*", "*");
		if(userInfo.getDeptId()!=null){
			//根据部门号得到部门信息和所属机构 
			DeptInfo deptInfo=deptService.getDeptInfo(userInfo.getDeptId());
			userInfo.setOrgId(deptInfo.getBelongOrg());
		}else if(StringUtils.isBlank(userInfo.getOrgId())){
			userInfo.setOrgId(UserCacheUtils.getOrgId());
		}
		
		
		
		model.put("deptYn", param.getParam1());
		model.put("userInfo", userInfo);
		return "user/list";
	}

	@RequestMapping("/getOrgChildren.do")
	public @ResponseBody Map<String, TreeNode> getOrgChildren(String orgId,String checkType) {
		OrgCacheInfo rootOrgCacheInfo = orgService.getOrgInfo(orgId);
		List<String> children = rootOrgCacheInfo.getChildOrgList();

		Map<String, TreeNode> childrenMap = new HashMap<String, TreeNode>();
		if (children != null) {
			for (String child : children) {
				childrenMap.put(child, buildTreeNode(child, "org"));
			}
		}
		
		if(!StringUtils.equals("org", checkType)){
			childrenMap.putAll(getOrgDepts(orgId));
		}
		return childrenMap;
	}

	@RequestMapping("/getOrgUsers.do")
	public @ResponseBody Map<String, TreeNode> getOrgUsers(String orgId,
			String deptId) {
		Map<String, TreeNode> childrenMap = new HashMap<String, TreeNode>();
		List<UserRelation> relations = userService.selectUserListByDept(orgId,
				deptId);

		if (relations != null) {
			for (UserRelation relation : relations) {
				UserCacheInfo userInfo = userService.getUserCacheInfo(relation
						.getUserId());
				if (userInfo != null) {
					childrenMap.put(userInfo.getUserId(),
							buildTreeNode(userInfo.getUserId(), "user"));
				}
			}
		}

		return childrenMap;
	}

	private Map<String, TreeNode> getOrgDepts(String orgId) {
		OrgCacheInfo rootOrgCacheInfo = orgService.getOrgInfo(orgId);
		List<String> subDepts = rootOrgCacheInfo.getChildDeptList();

		Map<String, TreeNode> childrenMap = new HashMap<String, TreeNode>();
		if (subDepts != null) {
			for (String dept : subDepts) {
				childrenMap.put(dept, buildTreeNode(dept, "dept"));
			}
		}

		return childrenMap;
	}
	private  TreeNode buildTreeNode(String id, String nodeType) {
		TreeNode node = new TreeNode();
		node.setId(id);
		//node.setPid(pid);
		node.setNodeType(nodeType);

		TreeNode.TreeNodeAttr attr = new TreeNode.TreeNodeAttr();

		if ("org".equals(nodeType)) {
			node.setText("<i class=\"" + TreeNode.ICON_ORG + "\"></i> " + orgService.getOrgName(id));
			node.setType("folder");
			attr.setDataIcon(TreeNode.ICON_ORG);
		} else if ("dept".equals(nodeType)) {
			node.setType("folder");
			node.setText("<i class=\"" + TreeNode.ICON_DEPT + "\"></i> "	+ deptService.getDeptName(id));
			attr.setDataIcon(TreeNode.ICON_DEPT);
		} else if ("user".equals(nodeType)) {
			node.setType("item");
			node.setText("<i class=\"" + TreeNode.ICON_USER + "\"></i> "	+ userService.getUserName(id));
			attr.setDataIcon("ace-icon fa fa-music blue");
			attr.setDataIcon(TreeNode.ICON_USER);
		}

		node.setTreeNodeAttr(attr);

		return node;
	}
	
	@RequestMapping("/toUserList.do")
	public String toUserList(ModelMap model) {
		return "user/userList";
	}

	
	@RequestMapping("selectUserList")
	@ResponseBody
	public Map<String, Object> selectUserList(@ModelAttribute UserInfo info,ModelMap model,String isSelect) {
		if (info == null) {
			info = new UserInfo();
		}
		//查询当前用户所属机构的用户
		if (StringUtils.isBlank(info.getOrgId())) {
			info.setOrgId(UserCacheUtils.getOrgId());
		}

		List<UserInfo> userList = userService.selectUserListPage(info);
		
		List<Object[]> list = new ArrayList<Object[]>();
		if (userList != null) {
			for (UserInfo u : userList) {
				Object[] arr = new Object[12];
				int index = 0;
				if(StringUtils.equals("1",isSelect)){
					arr = new Object[13];
					arr[index++] ="<div class=\"center\">"+ 
							"<label class=\"pos-rel\">"+
								"<input type=\"radio\" name=\"selectdUser\" class=\"ace\" onclick=\"selectUser('"+u.getUserId()+"','"+u.getUserName()+"','"+u.getRoles()+"','"+u.getOrgId()+"','"+u.getDeptId()+"')\"/>"+									
								"<span class=\"lbl\"></span>"+
							 "</label>"+
						"</div>";
				}
				arr[index++] = u.getUserId();
				arr[index++] = u.getUserCode();
				arr[index++] = u.getUserName();
				arr[index++] = orgCacheService.getOrgName(u.getOrgId());
				//arr[index++] = DictCacheUtils.getDictValue(u.getUserStatus(),"D00051");
				//arr[index++] = u.getCardNo();
				List<RoleInfo> roleList = u.getRoles();
				String str = "";
				if(roleList != null&&!roleList.isEmpty()){
					for(RoleInfo ri:roleList){
						str +=ri.getRoleName().trim()+ ",";
					}
				}
				arr[index++] = str.length() == 0 ? "" :str.substring(0, str.length() - 1);
				arr[index++] = DictCacheUtils.getDictValue(u.getStatus(),"D00026");
				list.add(arr);
			}
		}
		return DataTablesUtil.buildDataTablesReturn(list);
	}

	/**
	 * 跳转到新增页面   ----------------====
	 * 
	 * @return
	 */
	@RequestMapping("/toInsert.do")
	public String toInsert(HttpSession session, UserInfo userInfo, ModelMap model) {
		userInfo.setCreateUser(SessionContext.getUserId());
		userInfo.setCreateTime(DateUtil.getDateString(new Date()));
		List<RoleInfo> chooseRoleList = UserCacheUtils.getEnableRoleList();
		String defaultRoles = ConfigUtil.getConfig("defaultRoleId");
		
		userInfo.setRoles(chooseRoleList);
		model.put("userInfo",userInfo);
		model.put("roleList", chooseRoleList);
		model.put("operateType","insert");
		model.put("defaultRoles",defaultRoles);
		return "user/userInfoApply";
	}
	@RequestMapping("/insertUser.do")
	public @ResponseBody String insertUser(HttpSession session, UserInfo userInfo){
		String strValidate = userValidate(userInfo,"insert");
		if(org.apache.commons.lang.StringUtils.isNotBlank(strValidate)){
			return strValidate;
		}
		String message = userService.insertOrUpdateUserInfo(userInfo);
		String data;
		if ("success".equals(message)) {
			data = "{\"status\":\"ok\",\"message\":\"新增用户成功！\",\"retDeptId\":"+userInfo.getDeptId()+"}";
		}else {
			data ="{\"status\":\"fail\",\"message\":\"新增用户失败！\"}";
		}
		return data;
	}
	
	public String userValidate(UserInfo userInfo,String Flag){
		String data = "";
		if(org.apache.commons.lang.StringUtils.isNotBlank(userInfo.getUserCode())){
			if("insert".equals(Flag)){
				if (userService.getUserInfo(userInfo.getUserCode()) != null) {
					data = "{\"status\":\"fail\",\"message\":\"用户编码不能重复！\"}";
					return data;
				}
			}
			if("update".equals(Flag)){
				if(org.apache.commons.lang.StringUtils.isNotBlank(userInfo.getUserId())){
					if(!userInfo.getUserId().equals(userInfo.getUserCode())){
						data = "{\"status\":\"fail\",\"message\":\"用户ID和用户编码要相同！\"}";
						return data;
					}
				}else{
					data = "{\"status\":\"fail\",\"message\":\"用户ID不能为空！\"}";
					return data;
				}
			}
		}else{
			data = "{\"status\":\"fail\",\"message\":\"用户编码不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getUserName())){
			data = "{\"status\":\"fail\",\"message\":\"用户名不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getGender())){
			data = "{\"status\":\"fail\",\"message\":\"性别不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getCardType())){
			userInfo.setCardType("01");
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isNotBlank(userInfo.getCardNo())){
			UserInfo user = new UserInfo();
			if("insert".equals(Flag)){
				user.setFlag("insert");
			}
			if("update".equals(Flag)){
				user.setFlag("queryDetail");
			}
			user.setUserId(userInfo.getUserId());
			user.setCardType(userInfo.getCardType());
			user.setCardNo(userInfo.getCardNo());
			if(!validateCard(user)){
				data = "{\"status\":\"fail\",\"message\":\"身份证号码已经被使用！\"}";
				return data;
			}
		}else{
			data = "{\"status\":\"fail\",\"message\":\"身份证号码不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getOrgId())){
			data = "{\"status\":\"fail\",\"message\":\"机构编码不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getUserStatus())){
			data = "{\"status\":\"fail\",\"message\":\"是否在职不能为空！\"}";
			return data;
		}
		if(org.apache.commons.lang.StringUtils.isBlank(userInfo.getStatus())){
			data = "{\"status\":\"fail\",\"message\":\"用户状态不能为空！\"}";
			return data;
		}
		return data;
	}
	
	/**
	 * 跳转到个人菜单维护界面
	 * 
	 * @return
	 */
	@RequestMapping("/toUserMenuApply.do")
	public String toUserMenuApply(String relationId,ModelMap model) {
		/*String privateAllMenu = userService.getPrivateAllMenu(relationId);
		String privateMenuList = userService.getPrivateMenuList(relationId);
		String initMenuMessage = roleService.getInitMenu();
		model.put("privateAllMenu",privateAllMenu);
		model.put("privateMenuList",privateMenuList);
		model.put("initMenuMessage",initMenuMessage);
		return "user/privateMenu";*/
		/**
		 * @version 3.0
		 */
		 UserRelation ur=userService.selectByUserRelation(relationId);//
		 String roles=ur.getRoleIdstr();
		 model.put("roles", roles);
		 model.put("relationId", relationId);
		return "user/privateMenu";
	}
	/**
	 * 列出hw_role表中所有可用的角色
	 * @date 2016年1月15日 下午12:47:19
	 * @author 李仕波
	 */
	@RequestMapping("/listEnableRoles.do")
	@ResponseBody
	public   Map<String,Object> listEnableRoles(UserInfo userInfo,ModelMap model){
		HashMap<String,Object> map=new HashMap<String,Object>();
		List<RoleInfo> list=UserCacheUtils.getEnableRoleList();//可用角色
		List<RoleInfo> chooseRoleList=userService.selectUserInfo(userInfo).getRoles();//用户角色
		map.put("data", list);
		map.put("chooseRoleList", chooseRoleList);
		return map;
		
	}


	/**
	 * 修改首选
	 * 
	 * @return
	 */
	@RequestMapping("/updateReOrder.do")
	public @ResponseBody String updateReOrder(UserRelation userRelation){
		
		String message = userService.updateReOrder(userRelation);
		
		return message;
	}
	
	/**
	 * 重置密码
	 * @return
	 */
	@RequestMapping("/resetPwd.do")
	public @ResponseBody String[] resetPwd( UserInfo userInfo){
		String message = userService.resetPwd(userInfo);
		String[] data;
		if ("success".equals(message)) {
			data = new String[] { "ok", "重置密码成功！" };
		}else {
			data = new String[] { "fail", "重置密码失败！" };
		}
		return data;
	}

	/**
	 * 按用户编号，机构号，部门号查找关系表
	 * 
	 * @return
	 */
	@RequestMapping("/selectRelationBy3Id.do")
	public @ResponseBody String selectRelationBy3Id(UserRelation userRelation) {
		UserRelation ur = new UserRelation();
		ur.setUserId(userRelation.getUserId());
		ur.setOrgId(userRelation.getOrgId());
		ur.setDeptId(userRelation.getDeptId());
		String message = userService.selectRelationBy3Id(ur);
		return message;
	}

	/**
	 * 新增用户  
	 * SpringMVC同名参数绑定问题
	 */
	@InitBinder("userRelation")  
	public void initUserRelationBinder(WebDataBinder binder) {  
	    binder.setFieldDefaultPrefix("userRelation.");  
	} 

	@InitBinder("userInfo")  
	public void initUserInfoBinder(WebDataBinder binder) {  
	    binder.setFieldDefaultPrefix("userInfo.");  
	}
	/**
	 * 
	 * @param session
	 * @param userRelation
	 * @param userInfo
	 * @param userRoles
	 * @param defaultRole
	 * @return
	 */
	
	

	/**
	 * 删除用户
	 */
	@RequestMapping("/deleteUser.do")
	public @ResponseBody String deleteUser(UserInfo userInfo){
        String message = userService.deleteUserInfo(userInfo);
		String data="";
		if ("success".equals(message)) {
			data = "{\"status\":\"ok\",\"message\":\"成功删除用户！\"}";
		}else {
			data = "{\"status\":\"ok\",\"message\":\"删除用户失败！\"}";
		}
		return data;
	}
	
	/**
	 * 方法说明: 验证用户编号
	 * @param userCode
	 * @return
	 * @author liujix
	 */
	@RequestMapping("/validateCode")
	public @ResponseBody boolean validateCode(String userCode){
		if(userCode == null || userCode.isEmpty()){
			return false;
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserCode(userCode);
		List<UserInfo> userInfos = userService.selectUserMany(userInfo);
		if(userInfos == null || userInfos.isEmpty()){
			return true;
		}
		return false;
	}
	
	/**
	 * 根据证件类型和证件号
	 * @author 李仕波
	 * @date 2016年1月20日 下午8:37:56
	 * @version hiwan3.0
	 */
	@RequestMapping("/validateCard")
	public @ResponseBody boolean validateCard(UserInfo userInfo){
		
		// List<UserInfo> u=userService.selectUserMany(userInfo);
		if("insert".equals(userInfo.getFlag())){
			List<UserInfo> u=userService.selectUserMany(userInfo);
			if(u==null||u.isEmpty()){
				return true;
			}else{
				return false;
			}
		}else if("queryDetail".equals(userInfo.getFlag())){
			String userId=userInfo.getUserId();
			userInfo.setUserId("");
			List<UserInfo> u=userService.selectUserMany(userInfo);
			//
			if((u!=null&&!u.isEmpty()&&u.size()==1&&u.get(0).getUserId().equals(userId))||(u==null||u.isEmpty())){
				return true;
			}else {
				return false;
			}
		}
		return false;
	}
	/**
	 * 修改用户
	 */
	@RequestMapping("/updateUser.do")
	public @ResponseBody String updateUser(HttpSession session,UserInfo info){
		String data = null;
		try{
			String strValidate = userValidate(info,"update");
			if(org.apache.commons.lang.StringUtils.isNotBlank(strValidate)){
				return strValidate;
			}
			String message = userService.insertOrUpdateUserInfo(info);		
			
			if ("success".equals(message)) {			
				data = "{\"status\":\"ok\",\"message\":\"修改用户成功！\",\"retDeptId\":"+info.getDeptId()+"}";
			}else {			
				data = "{\"status\":\"fail\",\"message\":\"修改用户失败！\"}";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return data;
	}
	

	/**
	 * 跳转到查看详情页面
	 * 
	 * @return
	 */
	@RequestMapping("/toQueryDetail.do")
	public String toQuetyDetail(UserInfo info,String userFlag,ModelMap model){
		//SysParameterInfo ss = sysCacheService.getSysParameter("dept_yn", "*","*");
		UserInfo ur = userService.selectUserInfo(info);
		
		//DeptInfo deptInfo=deptService.getDeptInfo(ur.getDeptId());
		//OrgInfo orgInfo = OrgCacheUtils.get(deptInfo.getBelongOrg());
		List<RoleInfo> chooseRoleList = UserCacheUtils.getEnableRoleList();
		//StationInfo stationInfo = new StationInfo();
		//stationInfo.setDeptName(deptInfo.getDeptName());
		//stationInfo.setStatus("0");
		//stationInfo.setDeptLevel(orgInfo.getOrgLevel());
		//List<StationInfo> stationInfoList = stationInfoService.findList(stationInfo);
		
//		for (String str : proArray) {
//			userProducts.add(str);
//		}
		//model.put("deptYn", ss.getParam1());
		model.put("operateType", "queryDetail");
		model.put("userInfo", ur);
		model.put("roleList", chooseRoleList);
		
		//model.put("postList", JSONArray.fromObject(stationInfoList));
		if("1".equals(userFlag)){
			return "user/updateInfoApply";//个人信息维护
		}else{
			return "user/userInfoApply"; //用户信息
		}
	}
	
	/**
     * <p>fuel ux tree插件的数据源，一级菜单、二级菜单</p>
     * @param parentId
     * @return
     */

	/**
	 * 修改个人菜单
	 * 
	 * @return
	 */
	@RequestMapping("/userMenuApply.do")
	public @ResponseBody String userMenuApply(UserRelation userRelation,
			String privateMenuList) {
		String message = userService.updateUserMenu(userRelation.getRelationId(), privateMenuList);
		String data;
		if ("success".equals(message)) {
			data = "{\"status\":\"ok\",\"message\":\"更改用户菜单成功！\"}";
		}else {
			data ="{\"status\":\"fail\",\"message\":\"更改用户菜单失败！\"}";
		}
		return data;
	}

	/**
	 * 根据用户Id，判断是否已经存在
	 * 
	 * @return
	 */
	@RequestMapping("/userIdVal.do")
	public String userIdVal(UserInfo userInfo) {
		String message = "";
		if (userService.getUserInfo(userInfo.getUserId()) != null) {
			message = "1";
		} else {
			message = "0";
		}
		//return null;
		return message;
	}

	/**
	 * 按用户编号查找用户信息
	 */
	@RequestMapping("/selectUserInfoByUserId.do")
	public String selectUserInfoByUserId(UserInfo userInfo) {
		userInfo = userService.getUserInfo(userInfo.getUserId());
		return null;
	}

	/**
	 * 按证件类型，证件号判断唯一性
	 * 
	 * @return
	 */
	@RequestMapping("/userValByCard.do")
	public String userValByCard(UserInfo userInfo) {
		String message = "";
		if (userService.selectUserInfoByCard(userInfo) != null) {
			message = "1";
		} else {
			message = "0";
		}
		//return null;
		return message;
	}

	/**
	 * 按证件类型，证件号查找用户信息
	 */
	@RequestMapping("/selectUserInfoByCard.do")
	public String selectUserInfoByCard(UserInfo userInfo) {
		userInfo = userService.selectUserInfoByCard(userInfo);
		return null;
	}

	/**
	 * 判断别名唯一性
	 * 
	 * @return
	 */
	@RequestMapping("/validateUserName.do")
	public @ResponseBody String validateUserName(UserInfo userInfo) {
		String message = "";

		if (userService.isAliasUsed(userInfo.getUserName(),userInfo.getUserId())) {
			//message = "{\"status\":\"ok\",\"message\":\"用户名已被占用\"}";
			message="false"; //
		} else {

			//message = "{\"status\":\"fail\",\"message\":\"此用户可用\"}";
			message="true";
		}

		return message;
	}

	/**
	 * 跳转到修改密码界面
	 * 
	 * @return
	 */
	@RequestMapping("/toUpdatePwd.do")
	public String toUpdatePwd(String prompt, HttpSession session,String userId,ModelMap model) {
		if ("1".equals(prompt)) {
			session.setAttribute("parentName", "系统管理");
			session.setAttribute("menuName", "密码修改");
		}
		model.put("userId", userId);
		model.put("operateType", "modifyPwd");
		return "user/updatePwd";
	}
	/**
	 * 修改密码
	 * 
	 * @return
	 */
	@RequestMapping("updatePwd")
	public @ResponseBody String[] updatePwd(String userId,
			HttpSession session, String newPwd, String oldPwd) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		userInfo.setUserPwd(newPwd);
		String message = userService.updatePwd(userInfo, oldPwd);
		String data[];
		if ("success".equals(message)) {
			data = new String[] { "ok", "修改成功！" };
		} else if ("fail1".equals(message)) {
			data = new String[] { "fail", "原密码不正确，请重新输入原密码！" };
		} else {
			data = new String[] { "fail", "修改失败！" };
		}
		return data;
	}

	/**
	 * 跳转到个人信息维护界面
	 * @param session
	 * @param uodr
	 * @return
	 */
	@RequestMapping("/toPrivateInfoApply.do")
	public String toPrivateInfoApply(HttpSession session, UserInfo uodr,
			ModelMap model) {

		String orgId = UserCacheUtils.getOrgId();
		String userId = UserCacheUtils.getUser().getUserId();
		uodr.setUserId(userId);
		String orgId1 = uodr.getOrgId();
		if (orgId1 == null || "".equals(orgId1)) {
			if (uodr.getDeptId() != null && !"".equals(uodr.getDeptId())) {
				uodr.setOrgId(uodr.getDeptId().substring(0, 8));
			} else {
				uodr.setOrgId(orgId);
			}
		}
		List<UserInfo> userList = userService.selectUserListByUser(uodr);
		List<UserInfo> list = new ArrayList<UserInfo>();
		if (userList != null) {
			for (UserInfo info : userList) {
				info.setCardType(DictCacheUtils.getDictValue(info.getCardType(), "D00002"));
				info.setStatus(DictCacheUtils.getDictValue(info.getStatus(), "D00026"));
				info.setUserStatus(DictCacheUtils.getDictValue(info.getUserStatus(), "D00051"));
				info.setPostCategory(DictCacheUtils.getDictValue(info.getPostCategory(), "D00053"));
				List<RoleInfo> roleList = info.getRoles();
				String str = "";
				if(roleList != null&&!roleList.isEmpty()){
					for(RoleInfo ri:roleList){
						str +=ri.getRoleName().trim()+ ",";
					}
				}
				info.setQueryRole(str.length() == 0 ? "" :str.substring(0, str.length() - 1));
				list.add(info);
			}
		}
		model.put("userList", list);
		model.put("privateInfoApply", "true");
		return "user/privateInfoApply";
	}
	@RequestMapping(value="updateUserDefaultRole/{roleId}",method=RequestMethod.POST)
	public @ResponseBody ReturnInfo updateUserDefaultRole(@PathVariable("roleId") String roleId){
		ReturnInfo returnInfo=new ReturnInfo();
		try {
			UserInfo user = UserCacheUtils.getUser();
			user.setDefaultRole(roleId);
			int result = userService.updateUserDefaultRole(user);
			if (result > 0){
				returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
				returnInfo.setRetmsg(ReturnCodeEnum.success.getDesc());
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnInfo.setRetcode(ReturnCodeEnum.exception.getCode());
			returnInfo.setRetmsg(ReturnCodeEnum.exception.getDesc());
		}
		return returnInfo;
	}
	@RequestMapping("mobile/getUserAllInfo")
	public @ResponseBody Map<String,Object> getUserAllInfo(UserInfo user){
		Map<String,Object> map=new HashMap<String,Object>();
		ReturnInfo returnInfo=new ReturnInfo();
		try {
			List<UserInfo> userInfos=userService.getUserInfo(user);
			OrgInfo orgInfo=userService.getUserOrgInfo(user);
			returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
			returnInfo.setRetmsg(ReturnCodeEnum.success.getDesc());
			returnInfo.setResult(userInfos);
			map.put("data", returnInfo);
			map.put("orgInfo", orgInfo);
		} catch (Exception e) {
			e.printStackTrace();
			returnInfo.setRetcode(ReturnCodeEnum.error.getCode());
			returnInfo.setRetmsg(ReturnCodeEnum.error.getDesc());
			map.put("data", returnInfo);
		}
		return map;
	}
}
